Dictionaries¶
🎨 {}¶
meals = {
    'breakfast': 'oatmeal',
    'lunch': 'leftovers',
    'dinner': 'spaghetti'
}
type(meals)
dict
meals
{'breakfast': 'oatmeal', 'lunch': 'leftovers', 'dinner': 'spaghetti'}
meals['breakfast']
'oatmeal'
meals['lunch']
'leftovers'
meals['leftovers']
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Cell In[20], line 1 ----> 1 meals['leftovers'] KeyError: 'leftovers'
meals = {
    'breakfast': 'candy',
    'lunch': 'pizza',
    'breakfast': 'oatmeal',
    'dinner': 'sandwiches',
    'breakfast': 'wheaties'
}
meals
{'breakfast': 'wheaties', 'lunch': 'pizza', 'dinner': 'sandwiches'}
meals['lunch'] = 'salad'
meals
{'breakfast': 'wheaties', 'lunch': 'salad', 'dinner': 'sandwiches'}
meals['second breakfast'] = 'bagels'
meals
{'breakfast': 'wheaties',
 'lunch': 'salad',
 'dinner': 'sandwiches',
 'second breakfast': 'bagels'}
for meal, offering in meals.items():
    print(f'For {meal} you get {offering}. 😋') 
For breakfast you get wheaties. 😋 For lunch you get salad. 😋 For dinner you get sandwiches. 😋 For second breakfast you get bagels. 😋
list(meals.items())
[('breakfast', 'wheaties'),
 ('lunch', 'salad'),
 ('dinner', 'sandwiches'),
 ('second breakfast', 'bagels')]
for meal in meals: # iterate over keys
    print(meal)
breakfast lunch dinner second breakfast
for dish in meals.values():  # iterate over values
    print(dish)
wheaties salad sandwiches bagels
🖌 in + dict¶
hometowns = {
    'Dallin Oaks': 'Provo, UT',
    'Jeffery Holland': 'St George, UT',
    'Merril Bateman': 'Lehi, UT',
    'Cecil Samuelson': 'Salt Lake City, UT',
    'Kevin Worthen': 'Dragerton, UT',
    'Shane Reese': 'Logan, UT'
}
'Shane Reese' in hometowns
True
'Provo, UT' in hometowns
False
'Karl Maeser' in hometowns
False
for person in ['Kevin Worthen', 'Henry Eyring', 'Shane Reese', 'Ernest Wilkinson', 'Karl Maeser']:
    if person in hometowns:
        print(f'{person} was born in {hometowns[person]}.')
    else:
        print(f"I don't know where {person} was born.")
Kevin Worthen was born in Dragerton, UT. I don't know where Henry Eyring was born. Shane Reese was born in Logan, UT. I don't know where Ernest Wilkinson was born. I don't know where Karl Maeser was born.
🧑🏽🎨 The world needs more emojis¶
You are given a dictionary mapping words to emojis.
Replace all instances of a word with it's corresponding emoji. Ignore case.
So, given a dictionary
emojis = {'dog': '🐶', 'cat': '🐱', 'tree': '🌳', 'bird': '🐦'}
The phrase
My dog has fleas.
Becomes
My 🐶 has fleas.
🧑🏻🎨 Team Assignments¶
Community members want to register for the Rec Center sports teams.
You have a dictionary that maps age groups to team names.
Map a list of tuples containing a name and age to a list of tuples containing name and team.
To compute a participants age group, find the nearest multiple of 3 that is less than or equal to the participant age.
If the participant does not have an age-group assignment, they go in team "Old Fogies".
👨🏾🎨 Cipher¶
Given a dictionary mapping letters to other letters (called a "cipher"), encode a message.
Characters that are not in the cipher are preserved.
Extra credit: The cipher only contains lower-case letters, but you should encode upper-case letters also. Preserve casing.
Key Ideas¶
- dict- {}
- Iterating over the key-value pairs in a dictionary
- key in dict
- Lookup information using []